import * as ACTIONS from '../../actions/examination-analysis/examFlowAction';
import { ENUM_VIEW } from '../../../schema/constants';

/**
 * 数据视图默认状态
 * @type {Object}
 */
export const DEFAULT_DATA_STATE = {
	// 错误信息
	message: '',
	// 可选项
	options: [{
		ID: 103,
		label: '试卷'
	}, {
		ID: 102,
		label: '知识点'
	}, {
		ID: 106, 
		label: '题型'
	}, {
		ID: 104,
		label: '同步'
	}, {
		ID: 101,
		label: '教辅'
	}, {
		ID: 105,
		label: '专题'
	}, {
		ID: 0,
		label: '来源分布'
	}],
	// 当前选项
	currentOptionID: 103,
	// 人/题次 视图
	PV: true,
	// 人/题数 视图
	UV: false,
	// 时间颗粒度
	currentTimeUnit: ENUM_VIEW.DAY,
	// 试卷数据
	optExaminationPaperData: {
		isFetching: false,
		data: {}
	},
	// 知识点数据
	optKnowledgeData: {
		isFetching: false,
		data: {}
	},
	// 题型数据
	optQuestionData: {
		isFetching: false,
		data: {}
	},
	// 同步数据
	optSynchronizeData: {
		isFetching: false,
		data: {}
	},
	// 教辅数据
	optSupplementaryData: {
		isFetching: false,
		data: {}
	},
	// 专题数据
	optSpecialTopicData: {
		isFetching: false,
		data: {}
	},
	// 来源分布
	optSourceDistribution: {
		isFetching: false,
		data: {}
	}
};

export function examReducer(state = DEFAULT_DATA_STATE, action = {}) {
	let { type, payload } = action;
	switch(type) {
		case ACTIONS.CHANGE_OPTION: 
			return Object.assign({}, state, {
				currentOptionID: payload.option
			});
		case ACTIONS.CHANGE_TIME_UNIT:
			return Object.assign({}, state, {
				currentTimeUnit: payload.option
			});
		case ACTIONS.PV_ACTIVE:
			return Object.assign({}, state, {
				PV: true,
				UV: false
			});
		case ACTIONS.UV_ACTIVE:
			return Object.assign({}, state, {
				PV: false,
				UV: true
			});
		case ACTIONS.GET_DATA_REQUEST: 
		{
			let { key } = payload;
			return Object.assign({}, state, {
				[key]: {
					isFetching: true,
					data: {}
				}
			});
		}
		case ACTIONS.GET_DATA_SUCCESS: 
		{
			let { key, data } = payload;
			return Object.assign({}, state, {
				[key]: {
					isFetching: false,
					data
				}
			})
		}
		case ACTIONS.GET_DATA_FAILURE:
		{
			let { key, message } = payload;
			return Object.assign({}, state, {
				message,
				[key]: {
					isFetching: false,
					data: {}
				}
			})
		}
		default: 
			return state;
	}
};